package org.lex.library;

import java.util.Collection;
import java.util.Hashtable;

public class AbstractLibrary implements Library {
	private Hashtable<String, Object> data = null;

	public AbstractLibrary(String name, double version) {
		if (0 == name.length())
			// this will also throw a NullPointerException
			throw new IllegalArgumentException("name is empty");
		this.data = new Hashtable<String, Object>();
		this.set(NAME, name);
		this.set(VERSION, version);
	}

	protected void set(String property, Object value) {
		data.put(property, value);
	}

	public Number getVersion() {
		return (Number) this.get(VERSION);
	}

	public String getName() {
		return (String) this.get(NAME);
	}

	public Object get(String property) {
		return data.get(property);
	}

	public Collection<String> properties() {
		return data.keySet();
	}

	public int compareTo(Library o) {
		return this.getName().compareTo(o.getName());
	}

	public String toString() {
		return this.toString("[", ",", "]");
	}

	public String toString(String prefix, String separator, String postfix) {
		StringBuilder buf = new StringBuilder();
		buf.append(prefix);
		buf.append("name=").append(this.getName()).append(separator);
		buf.append("version=").append(this.getVersion());
		buf.append(postfix);
		return buf.toString();
	}
}
